import {ConnectDb} from '../localDatabase/connectDb.js'

let db = new ConnectDb()


// 获取全部信息的接口
export  async function  searchStoreAllList(store_name){
    let store =  await db.getObjectStore(store_name)
    let storeRequest = store.getAll()

    return new Promise((resolve,reject)=>{
        storeRequest.onsuccess = function(event){
            console.log('查询成功',storeRequest.result)
            resolve(storeRequest.result)
        }
        storeRequest.onerror = function(event){
            reject('查询失败')
        }
    })
}

// 新增  信息的接口
export async function addStoreItem(store_name,param) {
    let store =  await db.getObjectStore(store_name,'readwrite')
    let storeRequest = store.add(param)

    return new Promise((resolve,reject)=>{
        storeRequest.onsuccess = function(event){
            console.log('新增成功',storeRequest.result)
            resolve(storeRequest.result)
        }
        storeRequest.onerror = function(event){
            reject(event.target)
        }
    })
}


// 修改 信息的接口
export async function updateStoreItem(store_name, param) {
    let store =  await db.getObjectStore(store_name, 'readwrite')
    let storeRequest = store.put(param)

    return new Promise((resolve,reject)=>{
        storeRequest.onsuccess = function(event){
            console.log('修改成功',storeRequest.result)
            resolve(storeRequest.result)
        }
        storeRequest.onerror = function(event){
            reject('修改失败')
        }
    })
}


// 删除  信息
export async function deleteStoreItem(store_name,id) {
    let store =  await db.getObjectStore(store_name,'readwrite')
    let storeRequest = store.delete(id)

    return new Promise((resolve,reject)=>{
        storeRequest.onsuccess = function(event){
            console.log('删除成功',storeRequest.result)
            resolve(storeRequest.result)
        }
        storeRequest.onerror = function(event){
            reject('删除失败')
        }
    })
}

